package com.xiaoruyuan.firstday.service;

import com.alibaba.druid.util.StringUtils;
import com.xiaoruyuan.firstday.dao.MemberDAO;
import com.xiaoruyuan.firstday.domain.Member;
import com.xiaoruyuan.firstday.domain.exception.BusinessException;
import com.xiaoruyuan.firstday.domain.exception.MemberExceptionEnum;
import com.xiaoruyuan.firstday.domain.vo.MemberVo;
import com.xiaoruyuan.firstday.utils.SpringUtil;

/**
 * @author zhengqiang
 * @version 1.0
 * @Date 2021/8/3 14:34
 * @Description 会员service类
 */
public class MemberService {
    public static String MOBILE_FORMAT_MINUS = "****";
    public static String FILE_URL_PREFIX = "https://wm.ruyuan2020.com/attachs/";

    /**
     * 根据手机号查询会员信息
     *
     * @param mobile 手机号
     * @return 会员信息
     */
    public MemberVo findMemberInfoByMobile(String mobile) {
        MemberDAO memberDAO = SpringUtil.getBean(MemberDAO.class);
        Member member = memberDAO.findByMobile(mobile);
        return convertMemberToMemberVo(member);


    }

    /**
     * 格式化手机号码
     *
     * @param mobile 手机号
     * @return 格式化后的手机号码
     */
    private String formatMobile(String mobile) {
        return mobile.substring(0, 3) + MOBILE_FORMAT_MINUS + mobile.substring(7);
    }

    /**
     * 加载文件的访问地址
     *
     * @param filePath 文件路径
     * @return 访问地址
     */
    private String loadFileUrl(String filePath) {
        return FILE_URL_PREFIX + filePath;
    }

    /**
     * 把会员实体类转换成会员VO类
     * 这个方法的目的是减少会员信息的返回，避免过多的字段返回给前端
     *
     * @param member 会员实体类
     * @return 会员VO类
     */
    private MemberVo convertMemberToMemberVo(Member member) {
        MemberVo memberVo = new MemberVo();
        //格式化手机号
        String formatMobile = formatMobile(member.getMobile());
        //获取头像的全地址
        String faceUrl = loadFileUrl(member.getFace());
        //设置uid
        memberVo.setUid(member.getUid());
        //设置头像
        memberVo.setFace(faceUrl);
        //设置手机号
        memberVo.setMobile(formatMobile);
        //设置昵称
        memberVo.setNickname(member.getNickname());
        //设置微信unionid
        memberVo.setWxUnionid(member.getWxUnionid());
        //设置 性别
        memberVo.setSex(member.getSex());
        return memberVo;
    }

    /***
     * 根据手机号更新会员昵称
     * @param mobile 会员手机号
     * @param nickname 会员昵称
     */
    public void updateNicknameByMobile(String mobile, String nickname) {
        System.out.println("手机号为：" + mobile + "的用户传入的新昵称为：" + nickname);
        //校验昵称合法性
        checkNickname(nickname);
        MemberDAO memberDAO = SpringUtil.getBean(MemberDAO.class);
        Member member = memberDAO.findByMobile(mobile);
        if (member == null) {
            throw new BusinessException(MemberExceptionEnum.MEMBER_NOT_EXIST.getMessage(), MemberExceptionEnum.MEMBER_NOT_EXIST.getCode());
        }
        System.out.println("用户原昵称为：" + member.getNickname()+",新昵称为:"+nickname);
        member.setNickname(nickname);
        memberDAO.save(member);
    }

    /**
     * 校验昵称的合法性
     *
     * @param nickname 昵称
     */
    private void checkNickname(String nickname) {
        if (StringUtils.isEmpty(nickname)) {
            throw new BusinessException(MemberExceptionEnum.NICKNAME_NOT_NULL.getMessage(), MemberExceptionEnum.NICKNAME_NOT_NULL.getCode());
        } else if (nickname.length() > 18) {
            throw new BusinessException(MemberExceptionEnum.NICKNAME_LENGTH_TOO_LONG.getMessage(), MemberExceptionEnum.NICKNAME_LENGTH_TOO_LONG.getCode());
        }
    }

}
